package com.study.example.gameserver.module.scene.entity.impl;

import com.sparrow.common.ai.behaviortree.BehaviorContext;
import com.sparrow.common.ai.findpath.impl.AStar;
import com.sparrow.common.aoi.domain.EntityType;
import com.sparrow.common.map.Position;
import com.study.example.gameserver.module.scene.entity.IMonsterEntity;

import java.util.*;

/**
 * 场景怪物
 */
public class Monster implements IMonsterEntity {
    private int configId;
    private long id;
    private int hp;
    private int maxHp;
    private int sceneId;
    private int mapId;
    private int lineId;
    private Position pos;
    private int angle;
    private int aoiX;
    private int aoiY;
    private int atk;
    private int def;
    private BehaviorContext behaviorContext;
    private Position spawn;
    private float speed;
    private List<Position> roads = new ArrayList<>();
    private int roadIndex;
    private AStar AStar;
    private long targetId;


    private Map<Long, Long> attackMeIds = new HashMap<>();

    public Map<Long, Long> getAttackMeIds() {
        return attackMeIds;
    }

    public void setTargetId(long targetId) {
        this.targetId = targetId;
    }

    public long getTargetId() {
        return targetId;
    }

    public Monster() {
        this.AStar = new AStar();
    }

    public void setAStar(AStar AStar) {
        this.AStar = AStar;
    }

    public AStar getAStar() {
        return AStar;
    }

    public void setRoadIndex(int roadIndex) {
        this.roadIndex = roadIndex;
    }

    public int getRoadIndex() {
        return roadIndex;
    }

    public List<Position> getRoads() {
        return roads;
    }

    public void setRoads(List<Position> roads) {
        this.roads = roads;
    }

    @Override
    public int getHp() {
        return hp;
    }

    @Override
    public int getMaxHp() {
        return maxHp;
    }

    @Override
    public boolean isAlive() {
        return getHp() > 0;
    }

    @Override
    public int getMapId() {
        return mapId;
    }

    @Override
    public void setMapId(int mapId) {
        this.mapId = mapId;
    }

    @Override
    public int getSceneId() {
        return sceneId;
    }

    @Override
    public void setSceneId(int sceneId) {
        this.sceneId = sceneId;
    }

    @Override
    public int getLineId() {
        return lineId;
    }

    @Override
    public void setLineId(int lineId) {
        this.lineId = lineId;
    }

    @Override
    public Position getPos() {
        return pos;
    }

    @Override
    public void setPos(Position pos) {
        this.pos = pos;
    }

    @Override
    public int getAngle() {
        return angle;
    }

    @Override
    public void setAngle(int angle) {
        this.angle = angle;
    }

    @Override
    public long getAOIId() {
        return id;
    }

    @Override
    public int getAOIX() {
        return aoiX;
    }

    @Override
    public void setAOIX(int x) {
        this.aoiX = x;
    }

    @Override
    public void setAOIY(int y) {
        this.aoiY = y;
    }

    @Override
    public int getAOIY() {
        return aoiY;
    }

    @Override
    public EntityType getEntityType() {
        return EntityType.MONSTER;
    }

    public int getConfigId() {
        return configId;
    }

    public void setConfigId(int configId) {
        this.configId = configId;
    }

    public long getId() {
        return id;
    }

    @Override
    public Position getSpawn() {
        return spawn;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public void setMaxHp(int maxHp) {
        this.maxHp = maxHp;
    }

    public int getAoiX() {
        return aoiX;
    }

    public void setAoiX(int aoiX) {
        this.aoiX = aoiX;
    }

    public int getAoiY() {
        return aoiY;
    }

    @Override
    public BehaviorContext getBehaviorContext() {
        return behaviorContext;
    }

    @Override
    public void setBehaviorContext(BehaviorContext context) {
        this.behaviorContext = context;
    }

    @Override
    public void setAtk(int atk) {
        this.atk = atk;
    }

    @Override
    public int takeDamage(int dmg) {
        int currentHp = getHp();
        int newHp = currentHp - dmg;
        if (newHp < 0) {
            newHp = 0;
        }
        setHp(newHp);
        // 返回真实扣除的血量
        return currentHp - newHp;
    }

    @Override
    public int getAtk() {
        return atk;
    }

    @Override
    public void tick(long now, long delta) {

    }

    @Override
    public void setSpawn(Position spawn) {
        this.spawn = spawn;
    }

    @Override
    public float getSpeed() {
        return speed;
    }

    @Override
    public void setSpeed(float speed) {
        this.speed = speed;
    }

    public int getDef() {
        return def;
    }

    public void setDef(int def) {
        this.def = def;
    }

    public void updateAngle() {
        if (roads == null || roads.size() < 2) {
            return;
        }
        if (roadIndex >= roads.size() - 1) {
            return;
        }
        Position current = getPos();
        Position next = roads.get(roadIndex + 1);

        float dx = next.getX() - current.getX();
        float dz = next.getZ() - current.getZ();

        // Unity 的 forward 是 Z+ 方向，因此这里的 atan2 参数要是 (dx, dz)
        double radians = Math.atan2(dx, dz); // 返回 [-PI, PI]
        double degrees = Math.toDegrees(radians); // 转成角度 [-180, 180]

        if (degrees < 0) {
            degrees += 360; // 统一为 [0, 360)
        }

        this.angle = (int) degrees;

    }


}
